package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CustomerInfo;
import com.ruoyi.system.service.ICustomerInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户信息Controller
 *
 * @author LK
 * @date 2024-06-21
 */
@RestController
@RequestMapping("/system/info")
public class CustomerInfoController extends BaseController
{
    @Autowired
    private ICustomerInfoService customerInfoService;
    @Autowired
    private JdbcOperations jdbcTemplate;

    //维修处理占比环形图数据
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/queryDataForEcharts_WXCLZB")
    public List<Map<String, Object>>  queryDataForEcharts_WXCLZB(){
        String sql = "SELECT wo.repair_status as name,count(*) as value \n" +
                "FROM work_order_info wo\n" +
                "GROUP BY wo.repair_status";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
//        System.out.println(maps);
        return maps;
    }

    //配件数量分布图数据
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/queryDataForEcharts_PJSL")
    public List<Map<String, Object>>  queryDataForEcharts_PJSL(){
        String sql = "SELECT \n" +
                "ai.accessory_name as name,\n" +
                "ai.stock_input iVal,\n" +
                "ai.stock_output oVal,\n" +
                "ai.stock_quantity allVal\n" +
                "FROM accessory_info ai";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
//        System.out.println(maps);
        return maps;
    }

    //预约数量分布图数据   按照年份查询
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/queryDataForEcharts_YYSL/{dateYear}")
    public List<Map<String, Object>>  queryDataForEcharts_YYSL(@PathVariable("dateYear") String dateYear){
//        dateYear = "2023";
        String sql = "SELECT SUBSTRING(ca.appointment_time,1,7) dateNum,count(*) value\n" +
                "FROM customer_appointments ca\n" +
                "where SUBSTRING(ca.appointment_time,1,4)='"+dateYear+"'\n" +
                "GROUP BY SUBSTRING(ca.appointment_time,1,7)";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
//        System.out.println(maps);
        return maps;
    }

    //预约单，工作单，维修单分布图数据
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/queryDataForEcharts_DANZI")
    public JSONObject queryDataForEcharts_DANZI(){
        String yuyueNumSql = "SELECT count(*) yuyueNum from customer_appointments";
        String gongzuoNumSql = "SELECT count(*) gongzuoNum from work_order_info";

        String weixiuNumNumSql = "SELECT count(*) weixiuNum from work_order_info where repair_type = '1'";
        String baoyangNumSql = "SELECT count(*) baoyangNum from work_order_info where repair_type = '0'";
        List<Map<String, Object>> maps1 = jdbcTemplate.queryForList(yuyueNumSql);
        List<Map<String, Object>> maps2 = jdbcTemplate.queryForList(gongzuoNumSql);
        List<Map<String, Object>> maps3 = jdbcTemplate.queryForList(weixiuNumNumSql);
        List<Map<String, Object>> maps4 = jdbcTemplate.queryForList(baoyangNumSql);
        Object yuyueNum = maps1.get(0).get("yuyueNum");
        Object gongzuoNum = maps2.get(0).get("gongzuoNum");
        Object weixiuNum = maps3.get(0).get("weixiuNum");
        Object baoyangNum = maps4.get(0).get("baoyangNum");
        JSONObject returnObj = new JSONObject();
        returnObj.put("yuyueNum",yuyueNum);
        returnObj.put("gongzuoNum",gongzuoNum);
        returnObj.put("weixiuNum",weixiuNum);
        returnObj.put("baoyangNum",baoyangNum);
//        System.out.println(returnObj);
        return  returnObj;
    }

    /**
     * 查询客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerInfo customerInfo)
    {
        System.out.println("查询客户信息");
        startPage();
        List<CustomerInfo> list = customerInfoService.selectCustomerInfoList(customerInfo);
        return getDataTable(list);
    }

    /**
     * 导出客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerInfo customerInfo)
    {
        List<CustomerInfo> list = customerInfoService.selectCustomerInfoList(customerInfo);
        ExcelUtil<CustomerInfo> util = new ExcelUtil<CustomerInfo>(CustomerInfo.class);
        util.exportExcel(response, list, "客户信息数据");
    }

    /**
     * 获取客户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{customerId}")
    public AjaxResult getInfo(@PathVariable("customerId") String customerId)
    {
        return success(customerInfoService.selectCustomerInfoByCustomerId(customerId));
    }
    /**
     * 获取客户id
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/getId/{phoneNum}")
    public String getCustomerIdByPhone(@PathVariable String phoneNum)
    {
        System.out.println(phoneNum);
        return customerInfoService.selectCustomerIdByPhone(phoneNum);
    }

    /**
     * 新增客户信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerInfo customerInfo)
    {
        int i = customerInfoService.insertCustomerInfo(customerInfo);
        if(i==-1){
            return AjaxResult.error("手机号已经绑定其他用户");
        }else {
            return toAjax(i);
        }

    }

    /**
     * 修改客户信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerInfo customerInfo)
    {
        return toAjax(customerInfoService.updateCustomerInfo(customerInfo));
    }

    /**
     * 删除客户信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{customerIds}")
    public AjaxResult remove(@PathVariable String[] customerIds)
    {
        return toAjax(customerInfoService.deleteCustomerInfoByCustomerIds(customerIds));
    }
}
